Welcome Guest | Sign in | Register

Home > Java Programming > Variables and Loops > Questions and Answers

01. What happens when the following code is compiled and run.

Select the one correct answer.
for(int i = 2; i < 4; i++)
for(int j = 2; j < 4; j++)
assert i!=j : i;
A. The class compiles and runs, but does not print anything. B. The number 2 gets printed with AssertionError
C. compile error D. The number 3 gets printed with AssertionError

Answer and Explanation

Answer: The number 2 gets printed with AssertionError

Explanation:
When i and j are both 2, assert condition is false, and AssertionError gets generated. .

Report Errors

Name:

Loading...

VView Answer | RReport | DDiscuss in Forum
02. class A {
A(String s) {
}
A() {
}
}
1. class B extends A {
2. B() { }
3. B(String s) {
4. super(s);
5. }
6. void test() {
7. // insert code here
8. }
9. }

Which of the below code can be insert at line 7 to make clean
compilation ?
A. A a = new B(); B. A a = new B(5);
C. A a = new A(String s); D. All of the above

Answer and Explanation

Answer: A a = new B();

Explanation:
A a = new B(); is correct because anonymous inner classes are no different from any
other class when it comes to polymorphism.

Report Errors

Name:

Loading...

VView Answer | RReport | DDiscuss in Forum
03. What is the output for the below code ?

public class Test {
public static void main(String... args) {
int a =5 , b=6, c =7;
System.out.println("Value is "+ b +c);
System.out.println(a + b +c);
System.out.println("String "+(b+c));
}
}
A. Value is 67 18 String 13 B. Value is 13 18 String 13
C. Value is 13 18 String D. Compilation fails

Answer and Explanation

Answer: Compilation fails

Explanation:
If the left hand operand is not a String then + operator treat as plus BUT if left hand
operand is a String then + perform String concatenation.

Report Errors

Name:

Loading...

VView Answer | RReport | DDiscuss in Forum
04. What will be the result of compiling and run the following code:

public class Test {
public static void main(String... args) throws Exception {
Integer i = 34;
int l = 34;
if(i.equals(l)){
System.out.println(true);
}else{
System.out.println(false);
}
}
}
A. true B. false
C. Compile Error D. None of the above

Answer and Explanation

Answer: true

Explanation:
equals() method for the integer wrappers will only return true if the two primitive types
and the two values are equal.

Report Errors

Name:

Loading...

VView Answer | RReport | DDiscuss in Forum
05. What is the output for the below code ?

1. public class Test {
2. static int i =5;
3. public static void main(String... args) {
4. System.out.println(i++);
5. System.out.println(i);
6. System.out.println(++i);
7. System.out.println(++i+i++);
8.
9. }
10. }
A. 5 6 7 16 B. 6 6 6 16
C. 6 6 7 16 D. 5 6 6 16

Answer and Explanation

Answer: 5 6 7 16

Explanation:
i++ : print value then increment (postfix - increment happens after the value of the
variable is used) ++i : increment the print (prefix - increment happens before the value of the variable is used)

Report Errors

Name:

Loading...

VView Answer | RReport | DDiscuss in Forum
06. What is the output for the below code ?

1. public class Test {
2. public static void main(String... args) {
3. Integer i = 34;
4. String str = (i<21)?"jan":(i<56)?"feb":"march";
5. System.out.println(str);
6. }
7. }
A. Feb B. Jan
C. March D. Compilation fails with an error at line 4

Answer and Explanation

Answer: Feb

Explanation:
This is nested conditional with unbox. (i<21) is false goto (i<56), (i<56) is true so result is "feb".

Report Errors

Name:

Loading...

VView Answer | RReport | DDiscuss in Forum
07. What is the output for the below code ?

1. public class Test {
2. public static void main(String[] args){
3. byte i = 128;
4. System.out.println(i);
5. }
6. }
A. 128 B. 0
C. Compilation fails with an error at line 3 D. Compilation fails with an error at line 4

Answer and Explanation

Answer: Compilation fails with an error at line 3

Explanation:
byte can only hold up to 127. So compiler complain about possible loss of precision.

Report Errors

Name:

Loading...

VView Answer | RReport | DDiscuss in Forum
08. What is the output for the below code ?

1. public class Test {
2. int i=8;
3. int j=9;
4. public static void main(String[] args){
5. add();
6. }
7. public static void add(){
8. int k = i+j;
9. System.out.println(k);
10. }
11. }
A. 17 B. 0
C. Compilation fails with an error at line 5 D. Compilation fails with an error at line 8

Answer and Explanation

Answer: Compilation fails with an error at line 8

Explanation:
i and j are instance variable and attempting to access an instance variable from a static method. So Compilation fails .

Report Errors

Name:

Loading...

VView Answer | RReport | DDiscuss in Forum
09. public class Loop {
public static void main(String[] args){

for(int i=0;false;i++){
System.out.println("java");
}
}
}

What will be output of above program?
A. java B. Null
C. It will not print anything. D. Compiler error

Answer and Explanation

Answer: Compiler error

Explanation:
There is no explanation...

Report Errors

Name:

Loading...

VView Answer | RReport | DDiscuss in Forum
10. What will be output of following program?

public class Datatype {
public static void main(String[] args) {
byte b=127;
b++;
b++;
System.out.println(b);
}
}
A. 129 B. 2
C. -127 D. Compiler error

Answer and Explanation

Answer: -127

Explanation:
Range of byte data in java is -128 to 127. But byte data type in java is cyclic in nature.

Report Errors

Name:

Loading...

VView Answer | RReport | DDiscuss in Forum



Partner Sites
LucentBlackBoard.com                  SoftLucent.com                  LucentJobs.com
All rights reserved © 2012-2015 SoftLucent.